package com.dede.rxbase.activity;

import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.dede.rxbase.comm.FragmentLifecycle;
import com.dede.rxbase.config.BaseConfig;
import com.dede.rxbase.util.BLog;
import com.dede.rxbase.util.BarUtils;

/**
 * @author hsh
 *         2018/1/9 11:10.
 */
public abstract class BaseActivity extends AppCompatActivity {

    private View mContentView;
    private LinearLayout mRootView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        BaseConfig config = BaseConfig.getInstance();
        getSupportFragmentManager()
                .registerFragmentLifecycleCallbacks(FragmentLifecycle.getInstance(), false);

        BarUtils.setActionBarVisibility(this, !config.isNoActionBar());

        mRootView = new LinearLayout(this);
        mRootView.setOrientation(LinearLayout.VERTICAL);

        View titleView = createTitle();
        if (titleView != null) {
            mRootView.addView(titleView);
        }

        int layoutId = getLayoutId();
        mContentView = getLayoutInflater().inflate(layoutId, mRootView, false);
        mRootView.addView(mContentView, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));

        setContentView(mRootView);

        initView(savedInstanceState);

        BLog.log(this, "initData");
        initData();
    }

    /**
     * 初始化Title
     *
     * @return View
     */
    @Nullable
    protected View createTitle() {
        return null;
    }

    /**
     * 获取contentViewId
     *
     * @return int 布局资源id
     */
    @LayoutRes
    protected abstract int getLayoutId();

    protected void initView(@Nullable Bundle savedInstanceState) {
    }

    /**
     * 加载数据
     */
    public void initData() {
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        getSupportFragmentManager().unregisterFragmentLifecycleCallbacks(FragmentLifecycle.getInstance());
    }

    public View getContentView() {
        return mContentView;
    }

    public LinearLayout getRootView() {
        return mRootView;
    }

}
